package com.villaday.idf.activity;

import android.content.Intent;
import android.graphics.Rect;
import android.net.Uri;
import android.os.Build;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.villaday.idf.R;
import com.villaday.idf.adapter3.InstallationAdapter;
import com.villaday.idf.adapter3.InstallationNearAdapter;
import com.villaday.idf.base.BaseActivity;
import com.villaday.idf.model3.HotelDetailsEntity;
import com.villaday.idf.util.PhoneUtil;
import com.villaday.idf.util.ScrollViewUtil;
import com.villaday.idf.util.ToolBarUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


/**
 * 详情/设施
 */
public class InstallationActivity extends BaseActivity {


    @BindView(R.id.toolbar_back)
    ImageButton toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.toolbar_next)
    TextView toolbarNext;
    @BindView(R.id.Line_view)
    View LineView;
    @BindView(R.id.toolbar_text)
    Toolbar toolbarText;
    @BindView(R.id.tv_checkIn)
    TextView tvCheckIn;
    @BindView(R.id.tv_checkOut)
    TextView tvCheckOut;
    @BindView(R.id.tv_il_child)
    TextView tvIlChild;
    @BindView(R.id.tv_il_animal)
    TextView tvIlAnimal;
    @BindView(R.id.rv_il_base)
    RecyclerView rvIlBase;
    @BindView(R.id.tv_il_intro)
    TextView tvIlIntro;

    @BindView(R.id.rv_il_near)
    RecyclerView rvIlNear;
    @BindView(R.id.iv_il_border0)
    ImageView ivIlBorder0;
    @BindView(R.id.iv_il_border1)
    ImageView ivIlBorder1;
    @BindView(R.id.iv_il_border2)
    ImageView ivIlBorder2;
    @BindView(R.id.iv_il_border3)
    ImageView ivIlBorder3;
    @BindView(R.id.tv_il_wind_title)
    TextView tvIlWindTitle;
    @BindView(R.id.tv_il_wind_shop)
    TextView tvIlWindShop;
    @BindView(R.id.sul_installation)
    ScrollViewUtil sulInstallation;


    @BindView(R.id.tv_il_base)
    TextView tvIlBase;
    @BindView(R.id.tv_il_intro_title)
    TextView tvIlIntroTitle;
    @BindView(R.id.tv_il_near)
    TextView tvIlNear;
    @BindView(R.id.ll_il_border0)
    LinearLayout llIlBorder0;
    @BindView(R.id.ll_il_border1)
    LinearLayout llIlBorder1;
    @BindView(R.id.ll_il_border2)
    LinearLayout llIlBorder2;
    @BindView(R.id.ll_il_border3)
    LinearLayout llIlBorder3;
    @BindView(R.id.ll_install0)
    LinearLayout llInstall0;
    @BindView(R.id.ll_left)
    LinearLayout llLeft;
    @BindView(R.id.ll_right)
    LinearLayout llRight;
    @BindView(R.id.ll_install_title)
    LinearLayout llInstallTitle;
    @BindView(R.id.tv_il_phone0)
    TextView tvIlPhone0;
    @BindView(R.id.tv_il_phone1)
    TextView tvIlPhone1;
    @BindView(R.id.tv_il_phone2)
    TextView tvIlPhone2;
    @BindView(R.id.tv_il_phone3)
    TextView tvIlPhone3;
    @BindView(R.id.tv_il_phone4)
    TextView tvIlPhone4;
    @BindView(R.id.tv_il_phone5)
    TextView tvIlPhone5;
    @BindView(R.id.tv_il_phone6)
    TextView tvIlPhone6;


    private HotelDetailsEntity.BodyBean bodyBean;
    private boolean ischeck = false;
    private List<TextView> listText;
    private String[] phone;

    @Override
    protected int getViewResId() {
        return R.layout.activity_installation;
    }

    @Override
    protected void init() {
        super.init();
        try {
            ToolBarUtil.initToolBar(toolbarText, "详情/设施", new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                toolbarText.setElevation(PhoneUtil.dp2px(this, 3));
                llInstallTitle.setElevation(PhoneUtil.dp2px(this, 3));
            }

            //改了一个

            List<ImageView> list = new ArrayList<>();
            list.add(ivIlBorder0);
            list.add(ivIlBorder1);
            list.add(ivIlBorder2);
            list.add(ivIlBorder3);


            listText = new ArrayList<>();
            listText.add(tvIlPhone0);
            listText.add(tvIlPhone1);
            listText.add(tvIlPhone2);
            listText.add(tvIlPhone3);
            listText.add(tvIlPhone4);
            listText.add(tvIlPhone5);
            listText.add(tvIlPhone6);


            String data = getIntent().getStringExtra("data");
            if (data != null) {
                bodyBean = new Gson().fromJson(data, HotelDetailsEntity.BodyBean.class);
                if (bodyBean.getCheckInTime() != null && bodyBean.getCheckOutTime() != null) {
                    tvCheckIn.setText(bodyBean.getCheckInTime() + "以后");
                    tvCheckOut.setText(bodyBean.getCheckOutTime() + "以后");
                } else {
                    llLeft.setVisibility(View.GONE);
                    llRight.setVisibility(View.GONE);
                }


                tvIlIntro.setText(bodyBean.getIntroduce());
                if (bodyBean.getPhone() != null && bodyBean.getPhone().length() != 0) {
                    phone = bodyBean.getPhone().split(" ");

                    for (int i = 0; i < phone.length; i++) {
                        listText.get(i).setText(phone[i]);
                    }
                } else {

                }


                List<HotelDetailsEntity.BodyBean.PoliciesBean> policies = bodyBean.getPolicies();
                if (policies != null && policies.size() != 0) {
                    for (int i = 0; i < policies.size(); i++) {
                        if (policies.get(i).getName().contains("宠物")) {
                            tvIlAnimal.setText(policies.get(i).getContent());
                        }
                        if (policies.get(i).getName().contains("儿童")) {
                            tvIlChild.setText(policies.get(i).getContent());
                        }

                        if (policies.get(i).getName().contains("酒店")) {
                            tvIlWindTitle.setVisibility(View.VISIBLE);
                            tvIlWindShop.setVisibility(View.VISIBLE);
                            tvIlWindShop.setText(policies.get(i).getContent());
                        }
                    }
                }

                sulInstallation.setOnScollChangedListener(new ScrollViewUtil.OnScrollViewChangedListener() {
                    @Override
                    public void onScrollChanged(ScrollViewUtil scrollViewUtil, int x, int y, int oldx, int oldy) {
                        if (!ischeck) {
                            if (y == 0) {
                                btnOnclick(0);
                            } else if (isVisbility(tvIlNear)) {
                                btnOnclick(3);
                            } else if (isVisbility(tvIlIntroTitle)) {
                                btnOnclick(2);
                            } else if (isVisbility(tvIlBase)) {
                                btnOnclick(1);
                            }
                        } else {
                            ischeck = false;
                        }
                    }
                });

                //初始化列表
                initRecycle();
                //初始化底部列表
                initNearRecycle();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public void btnOnclick(int type) {
        ivIlBorder0.setVisibility(View.INVISIBLE);
        ivIlBorder1.setVisibility(View.INVISIBLE);
        ivIlBorder2.setVisibility(View.INVISIBLE);
        ivIlBorder3.setVisibility(View.INVISIBLE);
        ischeck = true;
        switch (type) {
            case 0:
                ivIlBorder0.setVisibility(View.VISIBLE);
                break;
            case 1:
                ivIlBorder1.setVisibility(View.VISIBLE);
                break;
            case 2:
                ivIlBorder2.setVisibility(View.VISIBLE);
                break;
            case 3:
                ivIlBorder3.setVisibility(View.VISIBLE);
                break;
        }
    }


    public boolean isVisbility(View view) {
        Rect scrollBounds = new Rect();
        sulInstallation.getHitRect(scrollBounds);
        if (view.getLocalVisibleRect(scrollBounds)) {
            //子控件至少有一个像素在可视范围内
            // Any portion of the childView, even a single pixel, is within the visible window
            return true;
        } else {
            //子控件完全不在可视范围内
            // NONE of the childView is within the visible window
            return false;
        }
    }


    /**
     * 初始化底部
     */
    private void initNearRecycle() {
        rvIlNear.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        InstallationNearAdapter installationAdapter = new InstallationNearAdapter(this);
        rvIlNear.setAdapter(installationAdapter);
        List<HotelDetailsEntity.BodyBean.AroundsBean> arounds = new ArrayList<>();
        if (null != bodyBean.getArounds() && bodyBean.getArounds().size() != 0) {
            for (HotelDetailsEntity.BodyBean.AroundsBean around : bodyBean.getArounds()) {
                if (around.getAroundDist() <= 3) {
                    arounds.add(around);
                }
            }
        }
        if (arounds.size() == 0) {
            tvIlNear.setVisibility(View.GONE);
        }

        installationAdapter.setData(arounds);
    }

    /**
     * 初始化列表
     */
    private void initRecycle() {
        rvIlBase.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        InstallationAdapter installationAdapter = new InstallationAdapter(this);
        List<HotelDetailsEntity.BodyBean.FacilitiesBean> facilities = bodyBean.getFacilities();
        rvIlBase.setAdapter(installationAdapter);
        installationAdapter.setData(facilities);
    }


    @OnClick({R.id.ll_il_border0, R.id.ll_il_border1, R.id.ll_il_border2, R.id.ll_il_border3, R.id.tv_il_phone0
            , R.id.tv_il_phone1, R.id.tv_il_phone2, R.id.tv_il_phone3, R.id.tv_il_phone4, R.id.tv_il_phone5, R.id.tv_il_phone6})
    public void btnOnclick(View view) {
        switch (view.getId()) {
            case R.id.ll_il_border0:
                btnOnclick(0);
                sulInstallation.setScrollY(0);
                break;
            case R.id.ll_il_border1:
                btnOnclick(1);
                sulInstallation.setScrollY(getHeight(llInstall0));
                break;
            case R.id.ll_il_border2:
                btnOnclick(2);
                sulInstallation.setScrollY(getHeight(llInstall0) + getHeight(tvIlBase) + getHeight(rvIlBase));
                break;
            case R.id.ll_il_border3:
                btnOnclick(3);
                sulInstallation.setScrollY(getHeight(llInstall0) + getHeight(tvIlBase) + getHeight(rvIlBase) + getHeight(tvIlPhone0) + getHeight(tvIlIntro));
                break;
            case R.id.tv_il_phone0:
                btnOnclickPhone(0);
                break;
            case R.id.tv_il_phone1:
                btnOnclickPhone(1);
                break;
            case R.id.tv_il_phone2:
                btnOnclickPhone(2);
                break;
            case R.id.tv_il_phone3:
                btnOnclickPhone(3);
                break;
            case R.id.tv_il_phone4:
                btnOnclickPhone(4);
                break;
            case R.id.tv_il_phone5:
                btnOnclickPhone(5);
                break;
            case R.id.tv_il_phone6:
                btnOnclickPhone(5);
                break;
        }
    }


    public void btnOnclickPhone(int position) {
        if (bodyBean.getPhone() != null) {
            if (phone.length <= listText.size()) {
                Intent intentPhone = new Intent(Intent.ACTION_DIAL);//调出拨号界面
                intentPhone.setData(Uri.parse("tel:" + phone[position]));
                startActivity(intentPhone);
            }
        }
    }


    public int getHeight(View view) {
        int width = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        int height = View.MeasureSpec.makeMeasureSpec(0,
                View.MeasureSpec.UNSPECIFIED);
        view.measure(width, height);
        view.getMeasuredWidth(); // 获取宽度
        view.getMeasuredHeight(); // 获取高度
        return view.getMeasuredHeight();
    }
}
